package com.li.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.eduservice.entity.EduChapter;
import com.li.eduservice.entity.EduVideo;
import com.li.eduservice.entity.chapter.ChapterVo;
import com.li.eduservice.entity.chapter.VideoVo;
import com.li.eduservice.mapper.EduChapterMapper;
import com.li.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.eduservice.service.EduVideoService;
import com.li.servicebase.exceptionhandler.XhuihuiException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-03-29
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {


    @Autowired
    private EduVideoService videoService;

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //根据课程Id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);

        //根据课程Id查询课程里面所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videoList = videoService.list(wrapperVideo);

        //创建list集合，用于最终封装数据
        List<ChapterVo> finalChapterList = new ArrayList<>();

        //遍历查询章节list集合
        for (int i = 0; i < chapterList.size(); i++) {
            //每个章节
            EduChapter chapter = chapterList.get(i);
            //chapter对象值复制到chapterVo
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            //把chapterVo放到finalList集合中
            finalChapterList.add(chapterVo);

            //创建集合 用于封装章节的小节
            List<VideoVo> finalVideoList = new ArrayList<>();
            //遍历查询小节list集合进行封装
            for (int j = 0; j < videoList.size(); j++) {
                //得到每个小节
                EduVideo video = videoList.get(j);
                //判断  下节里面chapter_id和章节里面的id是否一样
                if (video.getChapterId().equals(chapter.getId())){
                    //进行封装
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    //放到小节封装集合
                    finalVideoList.add(videoVo);
                }
            }
            //把封装之后小节list集合放到章节对象里面
            chapterVo.setChildren(finalVideoList);
        }

        return finalChapterList;
    }

    //删除章节方法  有小节删不掉
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapterId章节id查询小节表，如果查询数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if (count > 0){//查询处小节，不进行删除
            throw new XhuihuiException(20001,"不能删除");
        }else{//不能查询数据，进行删除
            //删除章节
            int i = baseMapper.deleteById(chapterId);
            //删除成功 1  不成功 0
            return i > 0;
        }

    }

    //2 根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
